<?php
X::import('Framework.XDatabase.Schema.XSqlRunner');

class MysqlSqlRunner extends XSqlRunner{
	/**
	 * @var resource
	 */
	public $result;
	
	/**
	 * @var resource
	 */
	public $conLink;

	/**
	 * @param string $sql
	 * @return MysqlSqlRunner
	 */
	function __construct( $conLink, $sql ){
		$this->conLink = $conLink;
		$this->result = mysql_query($sql);
		return $this;
	}
	
	/**
	 * @return integer
	 */
	public function affectedRowsCount(){
		return mysql_affected_rows();
	}
	
	/**
	 * @return integer
	 */
	public function resultRowsCount(){
		return mysql_num_rows($this->result);
	}
	
	/**
	 * @param integer $offset
	 * @return bool 
	 */
	public function dataSeek($offset){
		return mysql_data_seek($this->result, $offset);	
	}
	
	/**
	 * @param integer $type 
	 * @return array
	 */
	public function fetchArray($type=MYSQL_ASSOC){
		if ( MYSQL_ASSOC == $type )
			return mysql_fetch_assoc($this->result);
		else
			return mysql_fetch_row($this->result);
	}
	
	/**
	 * @param integer $offset
	 * @see XSqlRunner::fetchField()
	 */
	public function fetchField( $offset=-1 ){
		if (-1 == $offset )
			return mysql_fetch_field($this->result);
		else 
			return mysql_fetch_field($this->result, $offset);
	}
	
	/**
	 * @return array
	 */
	public function fetchLengths(){
		return mysql_fetch_lengths($this->result);
	}
	
	/**
	 * @return object
	 */
	public function fetchObject(){
		return mysql_fetch_object($this->result);
	}
	
	/**
	 * @param integer $offset
	 * @return array
	 */
	public function fieldFlags($offset){
		return explode(' ', mysql_field_flags($this->result, $offset));
	}
	
	/**
	 * @param integer $offset
	 * @return integer
	 */
	public function fieldLen($offset){
		return mysql_field_len($this->result, $offset);
	}
	
	/**
	 * @param integer $offset
	 * @return string
	 */
	public function fieldName($offset){
		return mysql_field_name($this->result, $offset);
	}
	
	/**
	 * @param integer $offset
	 * @return integer
	 */
	public function fieldSeek($offset){
		return mysql_field_seek($this->result, $offset);
	}
	
	/**
	 * @param integer $offset
	 * @return string
	 */
	public function fieldTable($offset){
		return mysql_field_table($this->result, $offset);
	}
	
	/**
	 * @param integer $offset
	 * @return string
	 */
	public function fieldType($offset){
		return mysql_field_type($this->result, $offset);
	}
	
	/**
	 * @return bool
	 */
	public function freeResult(){
		return mysql_free_result($this->result);
	}

	/**
     * @return string
	 */
	public function info(){
		return mysql_info($this->conLink);
	}
	
	/**
     * @return integer
	 */
	public function insertIDd(){
		return mysql_insert_id();
	}

	/**
	 * @return integer
	 */
	public function numFields(){
		return mysql_num_fields($this->result);
	}
	
	/**
	 * @param resource $result
	 * @param integer $row
	 * @param mixed field
	 * 
	 * @return string
	 */
	public function result( $result, $row, $field){
		return mysql_result($this->result, $row, $field);
	}
	
	/**
	 * @return array
	 */
	public function allResult(){
		$result = array();
		$len = $this->resultRowsCount();
		for ( $i=0; $i<$len; $i++ ){
			array_push($result, $this->fetchArray());
		}
		
		return $result;
	}
	
	/**
	 * @return interger
	 */
	public function errno(){
		return mysql_errno();
	}
	
	/**
	 * @return string
	 */
	public function error(){
		return mysql_error($this->conLink);
	}
	
	/**
	 * @return bool
	 */
	public function hasError(){
		return !(0 == $this->errno());
	}
}
